package com.fancy.learncenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.InviteAdapter;
import com.fancy.learncenter.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.adapter.base.CustomViewHold;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.InviteDataBean;
import com.fancy.learncenter.bean.InviteDetailDataBean;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.WXShareUtil;
import com.fancy.learncenter.view.PopupWindowResumeEdit;
import com.fancy.learncenter.view.PopupWindowShare;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.RongIM;

public class InviteDetailActivity extends BaseActivity {

    @Bind(R.id.recycleView)
    RecyclerView recycleView;

    View topView;


    String recruitId;
    @Bind(R.id.temp_ll)
    LinearLayout tempLl;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.rootView)
    LinearLayout rootView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite_detail, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("招聘详情");

        setRightImg(R.mipmap.ic_fenxiang, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindowShare == null) {
                    popupWindowShare = new PopupWindowShare(InviteDetailActivity.this, rootView);
                    popupWindowShare.setOnClickCallBack(new PopupWindowShare.OnClickCallBack() {
                        @Override
                        public void wxFriendCallBack() {
                            WXShareUtil.getInstance(InviteDetailActivity.this).shareWebPage(shareUrlPic, shareTitle, shareContent, shareLinkUrl, true);
                        }

                        @Override
                        public void wxCallBack() {
                            WXShareUtil.getInstance(InviteDetailActivity.this).shareWebPage(shareUrlPic, shareTitle, shareContent, shareLinkUrl, false);
                        }
                    });
                }
                popupWindowShare.showAtLocation();

            }
        });


        recruitId = getIntent().getStringExtra("recruitId");

        topView = LayoutInflater.from(this).inflate(R.layout.activity_invite_detail_top, null);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);
        inviteAdapter = new InviteAdapter(this, new ArrayList<InviteDataBean>());
        recycleView.setAdapter(inviteAdapter);
        inviteAdapter.setHeaderView(topView, recycleView);


        picRecycleView = topView.findViewById(R.id.pic_recycleView);

        work_name = topView.findViewById(R.id.work_name);
        view_num = topView.findViewById(R.id.view_num);
        price = topView.findViewById(R.id.price);
        work_experience = topView.findViewById(R.id.work_experience);
        location = topView.findViewById(R.id.location);
        education_experience = topView.findViewById(R.id.education_experience);
        company_name = topView.findViewById(R.id.company_name);
        work_name = topView.findViewById(R.id.work_name);
        location_detail = topView.findViewById(R.id.location_detail);
        salary_tv = topView.findViewById(R.id.salary_tv);
        description_tv = topView.findViewById(R.id.description_tv);


        //  getDatas();
        getDetailDatas();

    }

    PopupWindowShare popupWindowShare;

    String shareUrlPic = "";
    String shareLinkUrl = "";
    String shareTitle = "";
    String shareContent = "";

    RecyclerView picRecycleView;
    InviteAdapter inviteAdapter;
    TextView work_name;
    TextView view_num;
    TextView price;
    TextView work_experience;
    TextView location;
    TextView education_experience;
    TextView company_name;
    TextView location_detail;
    TextView salary_tv;
    TextView description_tv;

    private void getDatas() {
        Map map = new HashMap();

        HttpMehtod.getInstance().inviteList(map, new IdeaObserver<BaseDataBean<List<InviteDataBean>>>(this, true) {

            @Override
            public void onSuccess(BaseDataBean<List<InviteDataBean>> listBaseDataBean) {
                if (listBaseDataBean.getData() == null) {
                    return;

                }
                inviteAdapter.notifyDataSetChanged((ArrayList<InviteDataBean>) listBaseDataBean.getData());

            }

            @Override
            public void onFail(int errorCode) {

            }
        });

    }

    String userId;

    private void getDetailDatas() {
        Map map = new HashMap();
        map.put("recruitId", recruitId);
        HttpMehtod.getInstance().inviteDetail(map, new IdeaObserver<BaseDataBean<InviteDetailDataBean>>(this, true) {

            @Override
            public void onSuccess(BaseDataBean<InviteDetailDataBean> listBaseDataBean) {
                if (listBaseDataBean.getData() == null) {
                    return;

                }

                userId = listBaseDataBean.getData().getUser_id();
                work_name.setText(listBaseDataBean.getData().getCompany_name());
                view_num.setText("");
                price.setText(listBaseDataBean.getData().getSalary_name());

                work_experience.setText(listBaseDataBean.getData().getWorkyear_name());
                location.setText(listBaseDataBean.getData().getLocation_name());
                education_experience.setText(listBaseDataBean.getData().getEducation_name());

                company_name.setText(listBaseDataBean.getData().getCompany_name());

                location_detail.setText(listBaseDataBean.getData().getPlace());

                shareUrlPic = listBaseDataBean.getData().getShare_img();
                shareContent = listBaseDataBean.getData().getShare_content();
                shareLinkUrl = listBaseDataBean.getData().getShare_link();
                shareTitle = listBaseDataBean.getData().getShare_title();

                if (listBaseDataBean.getData().getWelfare_arr() != null && listBaseDataBean.getData().getWelfare_arr().size() > 0) {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int i = 0; i < listBaseDataBean.getData().getWelfare_arr().size(); i++) {
                        stringBuilder.append(listBaseDataBean.getData().getWelfare_arr().get(i) + "   ");
                    }
                    salary_tv.setText(stringBuilder.toString());
                }


                description_tv.setText(listBaseDataBean.getData().getContent());


                if (userId.equals(MyApplication.userId)) {
                    tempLl.setVisibility(View.VISIBLE);
                }


                if (listBaseDataBean.getData().getImglist() != null && listBaseDataBean.getData().getImglist().size() > 0) {

                    GridLayoutManager linearLayoutManager = new GridLayoutManager(InviteDetailActivity.this, 3);
                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    picRecycleView.setLayoutManager(linearLayoutManager);

                    detailPicData = (ArrayList<String>) listBaseDataBean.getData().getImglist();

                    picRecycleView.setAdapter(new CommonRecycleViewAdapter<String>(InviteDetailActivity.this, R.layout.wd__pic_item, (ArrayList<String>) listBaseDataBean.getData().getImglist()) {
                        @Override
                        public void bindView(CustomViewHold customViewHold, String item, final int position) {

                            SimpleDraweeView pic = customViewHold.getView(R.id.pic);
                            pic.setImageURI(item);
                            pic.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(InviteDetailActivity.this, GalleryPicDetailActivity.class);
                                    intent.putExtra(GalleryPicDetailActivity.PIC_KEY, detailPicData);
                                    intent.putExtra(GalleryPicDetailActivity.PIC_POSITION_KEY, position);
                                    startActivity(intent);

                                }

                            });


                        }
                    });


                }
            }

            @Override
            public void onFail(int errorCode) {

            }
        });

    }

    ArrayList<String> detailPicData;

    private void applyInvite() {
        Map map = new HashMap();
        map.put("recruitId", recruitId);
        HttpMehtod.getInstance().applyInvite(map, new IdeaObserver<BaseDataBean<InviteDetailDataBean>>(this, true) {

            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {
                ToastUtil.show("投递成功");
            }

            @Override
            public void onFail(int errorCode) {

                if (errorCode == 1000) {
                    if (popupWindowResumeEdit == null) {
                        popupWindowResumeEdit = new PopupWindowResumeEdit(InviteDetailActivity.this, toolbar);
                    }
                    popupWindowResumeEdit.isShow();
                }


            }
        });

    }

    PopupWindowResumeEdit popupWindowResumeEdit;

    @OnClick({R.id.msg_tv, R.id.apply_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.msg_tv:
                RongIM.getInstance().startPrivateChat(this, userId, "");
                break;
            case R.id.apply_tv:
                applyInvite();
                break;
        }
    }
}
